#include <stdio.h>

void length()
{
    /*1169:	f3 0f 1e fa          	endbr64 
    116d:	55                   	push   %rbp
    116e:	48 89 e5             	mov    %rsp,%rbp
    1171:	48 83 ec 30          	sub    $0x30,%rsp
    1175:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    117c:	00 00 
    117e:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    1182:	31 c0                	xor    %eax,%eax*/

    /*1184:	be 28 00 00 00       	mov    $0x28,%esi		// 40 ---> 5指针，因每个指针占8个字节 (64bit系统)
    1189:	48 8d 3d 74 0e 00 00 	lea    0xe74(%rip),%rdi        # 2004 <_IO_stdin_used+0x4>
    1190:	b8 00 00 00 00       	mov    $0x0,%eax
    1195:	e8 d6 fe ff ff       	callq  1070 <printf@plt>*/
	printf("sizeof(*pa[5]) = %d\n", 40); 

    /*119a:	be 08 00 00 00       	mov    $0x8,%esi		// 8 ---> 1个指针，占8字节 (64bit系统)
    119f:	48 8d 3d 74 0e 00 00 	lea    0xe74(%rip),%rdi        # 201a <_IO_stdin_used+0x1a>
    11a6:	b8 00 00 00 00       	mov    $0x0,%eax
    11ab:	e8 c0 fe ff ff       	callq  1070 <printf@plt>*/
	printf("sizeof((*ptr)[5]) = %d\n", 8);

    /*11b0:	90                   	nop
    11b1:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    11b5:	64 48 33 04 25 28 00 	xor    %fs:0x28,%rax
    11bc:	00 00 
    11be:	74 05                	je     11c5 <length+0x5c>
    11c0:	e8 9b fe ff ff       	callq  1060 <__stack_chk_fail@plt>
    11c5:	c9                   	leaveq 
    11c6:	c3                   	retq  */
}

void pointer_array()
{
    /*11c7:	f3 0f 1e fa          	endbr64 
    11cb:	55                   	push   %rbp
    11cc:	48 89 e5             	mov    %rsp,%rbp
    11cf:	48 83 ec 40          	sub    $0x40,%rsp
    11d3:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    11da:	00 00 
    11dc:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    11e0:	31 c0                	xor    %eax,%eax
    11e2:	c7 45 d0 01 00 00 00 	movl   $0x1,-0x30(%rbp)
    11e9:	c7 45 d4 02 00 00 00 	movl   $0x2,-0x2c(%rbp)
    11f0:	c7 45 d8 03 00 00 00 	movl   $0x3,-0x28(%rbp)
    11f7:	c7 45 dc 04 00 00 00 	movl   $0x4,-0x24(%rbp)
    11fe:	c7 45 e0 05 00 00 00 	movl   $0x5,-0x20(%rbp)
    1205:	c7 45 e4 06 00 00 00 	movl   $0x6,-0x1c(%rbp)
    120c:	c7 45 e8 07 00 00 00 	movl   $0x7,-0x18(%rbp)
    1213:	c7 45 ec 08 00 00 00 	movl   $0x8,-0x14(%rbp)
    121a:	c7 45 f0 09 00 00 00 	movl   $0x9,-0x10(%rbp)*/
	int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    /*1221:	48 8d 45 d0          	lea    -0x30(%rbp),%rax
    1225:	48 89 45 c8          	mov    %rax,-0x38(%rbp)*/	// 一个指针
	int (*ptr)[3] = a;

    /*1229:	48 8b 45 c8          	mov    -0x38(%rbp),%rax	// rax=ptr
    122d:	8b 50 08             	mov    0x8(%rax),%edx	// edx=rax+8 --> 3
    1230:	48 8b 45 c8          	mov    -0x38(%rbp),%rax	// rax=ptr
    1234:	8b 40 04             	mov    0x4(%rax),%eax	// eax=rax+4 --> 2
    1237:	89 c6                	mov    %eax,%esi		// esi=eax
    1239:	48 8d 3d f3 0d 00 00 	lea    0xdf3(%rip),%rdi        # 2033 <_IO_stdin_used+0x33>
    1240:	b8 00 00 00 00       	mov    $0x0,%eax
    1245:	e8 26 fe ff ff       	callq  1070 <printf@plt>*/
	printf("%d %d\n", (*ptr)[1], (*ptr)[2]);    // 2    3

    /*124a:	48 83 45 c8 0c       	addq   $0xc,-0x38(%rbp)*/	// rbp-0x38+=0xc --> 跳过前三个(1,2,3)
	++ptr;

    /*124f:	48 8b 45 c8          	mov    -0x38(%rbp),%rax		// rax=ptr --> 4
    1253:	8b 50 08             	mov    0x8(%rax),%edx		// edx=rax+8 ---> 6
    1256:	48 8b 45 c8          	mov    -0x38(%rbp),%rax		// rax=ptr
    125a:	8b 40 04             	mov    0x4(%rax),%eax		// eax=rax+4 ---> 5
    125d:	89 c6                	mov    %eax,%esi
    125f:	48 8d 3d cd 0d 00 00 	lea    0xdcd(%rip),%rdi        # 2033 <_IO_stdin_used+0x33>
    1266:	b8 00 00 00 00       	mov    $0x0,%eax
    126b:	e8 00 fe ff ff       	callq  1070 <printf@plt>*/
	printf("%d %d\n", (*ptr)[1], (*ptr)[2]);    // 5    6

    /*1270:	90                   	nop
    1271:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1275:	64 48 33 04 25 28 00 	xor    %fs:0x28,%rax
    127c:	00 00 
    127e:	74 05                	je     1285 <pointer_array+0xbe>
    1280:	e8 db fd ff ff       	callq  1060 <__stack_chk_fail@plt>
    1285:	c9                   	leaveq 
    1286:	c3                   	retq   */
}

void array_pointer()
{
    /*1287:	f3 0f 1e fa          	endbr64 
    128b:	55                   	push   %rbp
    128c:	48 89 e5             	mov    %rsp,%rbp
    128f:	48 83 ec 60          	sub    $0x60,%rsp
    1293:	64 48 8b 04 25 28 00 	mov    %fs:0x28,%rax
    129a:	00 00 
    129c:	48 89 45 f8          	mov    %rax,-0x8(%rbp)
    12a0:	31 c0                	xor    %eax,%eax
    12a2:	c7 45 d0 01 00 00 00 	movl   $0x1,-0x30(%rbp)
    12a9:	c7 45 d4 02 00 00 00 	movl   $0x2,-0x2c(%rbp)
    12b0:	c7 45 d8 03 00 00 00 	movl   $0x3,-0x28(%rbp)
    12b7:	c7 45 dc 04 00 00 00 	movl   $0x4,-0x24(%rbp)
    12be:	c7 45 e0 05 00 00 00 	movl   $0x5,-0x20(%rbp)
    12c5:	c7 45 e4 06 00 00 00 	movl   $0x6,-0x1c(%rbp)
    12cc:	c7 45 e8 07 00 00 00 	movl   $0x7,-0x18(%rbp)
    12d3:	c7 45 ec 08 00 00 00 	movl   $0x8,-0x14(%rbp)
    12da:	c7 45 f0 09 00 00 00 	movl   $0x9,-0x10(%rbp)*/
	int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

	int *pa[3];
    /*12e1:	48 8d 45 d0          	lea    -0x30(%rbp),%rax		// rax=pa
    12e5:	48 89 45 b0          	mov    %rax,-0x50(%rbp)*/	// 第一个指针
	pa[0] = a[0];	// 1

    /*12e9:	48 8d 45 d0          	lea    -0x30(%rbp),%rax		// rax=pa
    12ed:	48 83 c0 0c          	add    $0xc,%rax			// rax=rax+0xc
    12f1:	48 89 45 b8          	mov    %rax,-0x48(%rbp)*/	// 第二个指针
	pa[1] = a[1];	// 4

    /*12f5:	48 8d 45 d0          	lea    -0x30(%rbp),%rax		// rax=pa
    12f9:	48 83 c0 18          	add    $0x18,%rax			// rax=rax+0x18
    12fd:	48 89 45 c0          	mov    %rax,-0x40(%rbp)*/	// 第三个指针
	pa[2] = a[2];	// 7

    /*1301:	c7 45 ac 00 00 00 00 	movl   $0x0,-0x54(%rbp)*/
	int i = 0;

    /*1308:	eb 23                	jmp    132d <array_pointer+0xa6>
    130a:	8b 45 ac             	mov    -0x54(%rbp),%eax		// eax=i
    130d:	48 98                	cltq   
    130f:	48 8b 44 c5 b0       	mov    -0x50(%rbp,%rax,8),%rax	// rax=(rbp+rax*8)-0x50, 分别取到指针pa[0], pa[1], pa[2]
    1314:	8b 00                	mov    (%rax),%eax				// eax=(rax)
    1316:	89 c6                	mov    %eax,%esi				// esi=eax
    1318:	48 8d 3d 1b 0d 00 00 	lea    0xd1b(%rip),%rdi        # 203a <_IO_stdin_used+0x3a>
    131f:	b8 00 00 00 00       	mov    $0x0,%eax
    1324:	e8 47 fd ff ff       	callq  1070 <printf@plt>
    1329:	83 45 ac 01          	addl   $0x1,-0x54(%rbp)			// i+1
    132d:	83 7d ac 02          	cmpl   $0x2,-0x54(%rbp)			// 2?=i
    1331:	7e d7                	jle    130a <array_pointer+0x83>*/
	for(i = 0; i < 3; i++)
		printf("%d ", *pa[i]);		// 1 4 7

    /*1333:	90                   	nop
    1334:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
    1338:	64 48 33 04 25 28 00 	xor    %fs:0x28,%rax
    133f:	00 00 
    1341:	74 05                	je     1348 <array_pointer+0xc1>
    1343:	e8 18 fd ff ff       	callq  1060 <__stack_chk_fail@plt>
    1348:	c9                   	leaveq 
    1349:	c3                   	retq   */
}

int main()
{
    /*134a:	f3 0f 1e fa          	endbr64 
    134e:	55                   	push   %rbp
    134f:	48 89 e5             	mov    %rsp,%rbp
    1352:	b8 00 00 00 00       	mov    $0x0,%eax
    1357:	e8 0d fe ff ff       	callq  1169 <length>*/
	length();

    /*135c:	b8 00 00 00 00       	mov    $0x0,%eax
    1361:	e8 61 fe ff ff       	callq  11c7 <pointer_array>*/
	pointer_array();

    /*1366:	b8 00 00 00 00       	mov    $0x0,%eax
    136b:	e8 17 ff ff ff       	callq  1287 <array_pointer>*/
	array_pointer();

    /*1370:	b8 00 00 00 00       	mov    $0x0,%eax
    1375:	5d                   	pop    %rbp
    1376:	c3                   	retq   
    1377:	66 0f 1f 84 00 00 00 	nopw   0x0(%rax,%rax,1)
    137e:	00 00 */
	return 0;
}
